﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ABELGX
{
    public static class Serialize
    {
        #region 串行序列化
        public static void SerializeBinary(object obj, string path)
        {
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(path, FileMode.Create);
                SerializeBinary(obj, fileStream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Flush();
                    fileStream.Close();
                    fileStream = null;
                }
            }
        }
        public static void SerializeBinary(object obj, Stream stream)
        {
            BinaryFormatter b = new BinaryFormatter();
            try
            {
                b.Serialize(stream, obj);
            }
            catch
            {
                throw;
            }
            finally
            {
                b = null;
            }
        }
        public static object DeSerializeBinary(string path)
        {
            object result = null;
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                result = DeSerializeBinary(fileStream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream = null;
                }
            }
            return result;
        }
        public static object DeSerializeBinary(Stream stream)
        {
            object result = null;
            BinaryFormatter b = null;
            try
            {
                b = new BinaryFormatter();
                result = b.Deserialize(stream);
            }
            catch
            {
                throw;
            }
            finally
            {
                b = null;
            }
            return result;
        }
        public static object CopyObject(Object obj)
        {
            object result = null;

            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream();
                SerializeBinary(obj, stream);
                stream.Position = 0;
                result = DeSerializeBinary(stream);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                    stream = null;
                }
            }
            return result;
        }
        #endregion

        #region XML序列化
        public static void SerializeXML<T>(T obj, string path)
        {
            Stream fileStream = null;
            try
            {
                fileStream = File.Create(path);
                XmlWriter textWriter = XmlWriter.Create(fileStream, new XmlWriterSettings()
                {
                    Indent = true,
                    Encoding = Encoding.UTF8,
                });
                SerializeXML(obj, textWriter);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Flush();
                    fileStream.Close();
                    fileStream = null;
                }
            }
        }
        public static void SerializeXML<T>(T obj, XmlWriter stream)
        {
            XmlSerializer b = new XmlSerializer(typeof(T));
            try
            {
                var ns = new XmlSerializerNamespaces();
                ns.Add("", "");

                b.Serialize(stream, obj, ns);
            }
            catch
            {
                throw;
            }
            finally
            {
                b = null;
            }
        }
        public static T DeSerializeXML<T>(string path)
        {
            T result = default(T);
            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                XmlReader textReader = XmlReader.Create(fileStream, new XmlReaderSettings());
                result = DeSerializeXML<T>(textReader);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream = null;
                }
            }
            return result;
        }
        public static T DeSerializeXML<T>(XmlReader stream)
        {
            T result = default(T);
            XmlSerializer b = new XmlSerializer(typeof(T));
            try
            {
                result = (T)b.Deserialize(stream);
            }
            catch
            {
                throw;
            }
            finally
            {
                b = null;
            }
            return result;
        }
        #endregion
    }
}
